/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lab2;

import actions.approve.ApproveAction;
import actions.approve.RejectAction;
import actions.edit.AddTaskAction;
import actions.edit.ChangeTaskAction;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import models.ApproveWindowModel;
import models.EditWindowModel;

/**
 *
 * @author Кирилл
 */
public final class ApproveWindow extends javax.swing.JDialog {

    /**
     * Creates new form AddWindow
     */
    private ApproveWindowModel approveModel = new ApproveWindowModel();

    public ApproveWindow(java.awt.Frame parent, boolean modal) {
        super(parent, modal);

        initComponents();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        approveButton = new javax.swing.JButton();
        taskName = new javax.swing.JTextField();
        rejectButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        taskDescription = new javax.swing.JTextArea();
        taskStartDay = new javax.swing.JComboBox();
        taskStartMonth = new javax.swing.JComboBox();
        taskStartYear = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        taskEndDay = new javax.swing.JComboBox();
        taskEndMonth = new javax.swing.JComboBox();
        taskEndYear = new javax.swing.JComboBox();
        taskStartHour = new javax.swing.JFormattedTextField();
        taskStartMinute = new javax.swing.JFormattedTextField();
        taskEndHour = new javax.swing.JFormattedTextField();
        taskEndMinute = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        actorField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        approveButton.setText("Approve");
        approveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                approveButtonActionPerformed(evt);
            }
        });

        taskName.setEditable(false);
        taskName.setText("New Task");

        rejectButton.setText("Reject");
        rejectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rejectButtonActionPerformed(evt);
            }
        });

        taskDescription.setEditable(false);
        taskDescription.setColumns(20);
        taskDescription.setRows(5);
        taskDescription.setText("Description\n");
        jScrollPane1.setViewportView(taskDescription);

        taskStartDay.setEnabled(false);

        taskStartMonth.setModel(new DefaultComboBoxModel(Helper.getMonths()));
        taskStartMonth.setEnabled(false);

        taskStartYear.setModel(new DefaultComboBoxModel(Helper.getYears(2012, 2015)));
        taskStartYear.setEnabled(false);

        jLabel1.setText(":");

        taskEndDay.setEnabled(false);

        taskEndMonth.setModel(new DefaultComboBoxModel(Helper.getMonths()));
        taskEndMonth.setEnabled(false);

        taskEndYear.setModel(new DefaultComboBoxModel(Helper.getYears(2012, 2015)));
        taskEndYear.setEnabled(false);

        taskStartHour.setEditable(false);
        taskStartHour.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#, ##"))));

        taskStartMinute.setEditable(false);

        taskEndHour.setEditable(false);
        taskEndHour.setText("jFormattedTextField3");

        taskEndMinute.setEditable(false);
        taskEndMinute.setText("jFormattedTextField4");

        jLabel3.setText("Actor for this tasl");

        actorField.setEditable(false);
        actorField.setText("Actor");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(taskName, javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(taskStartDay, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(taskEndDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(taskStartHour, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(taskEndMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(taskStartMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(taskStartMinute, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jLabel1)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(taskEndHour, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(4, 4, 4)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(taskStartYear, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(taskEndYear, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(layout.createSequentialGroup()
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(taskEndMinute, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGap(2, 2, 2)))
                            .addComponent(jLabel3)
                            .addComponent(actorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(approveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(rejectButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(actorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(taskName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(taskStartHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskStartMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskEndHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskEndMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(taskStartDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskStartMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskStartYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(taskEndMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskEndDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(taskEndYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rejectButton)
                    .addComponent(approveButton))
                .addGap(10, 10, 10))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void approveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_approveButtonActionPerformed
        // TODO add your handling code here:
        new ApproveAction().doAction(approveModel);
        this.setVisible(false);
    }//GEN-LAST:event_approveButtonActionPerformed

    private void rejectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rejectButtonActionPerformed
        // TODO add your handling code here:
        new RejectAction().doAction(approveModel);
        this.setVisible(false);
    }//GEN-LAST:event_rejectButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ApproveWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ApproveWindow dialog = new ApproveWindow(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });

    }

    public void recieveData(Task data) {

        approveModel.setTask(data);


        actorField.setText(approveModel.getTask().getActor());
        taskName.setText(approveModel.getTask().getName());
        taskDescription.setText(approveModel.getTask().getDescription());

        Calendar c = approveModel.getTask().getStime();
        String hour = new Integer(c.get(Calendar.HOUR_OF_DAY)).toString();
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        taskStartHour.setValue(hour);
        String minute = new Integer(c.get(Calendar.MINUTE)).toString();
        if (minute.length() == 1) {
            minute = "0" + minute;
        }
        taskStartMinute.setValue(minute);
        int month = c.get(Calendar.MONTH);
        taskStartMonth.setSelectedIndex(month);
        Integer year = c.get(Calendar.YEAR);
        taskStartYear.setSelectedItem(year.toString());
        taskStartDay.setSelectedIndex(c.get(Calendar.DATE) - 1);

        c = approveModel.getTask().getFtime();
        hour = new Integer(c.get(Calendar.HOUR_OF_DAY)).toString();
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        minute = new Integer(c.get(Calendar.MINUTE)).toString();
        if (minute.length() == 1) {
            minute = "0" + minute;
        }
        taskEndHour.setValue(hour);
        taskEndMinute.setValue(minute);
        month = c.get(Calendar.MONTH);
        taskEndMonth.setSelectedIndex(month);
        year = c.get(Calendar.YEAR);
        taskEndYear.setSelectedItem(year.toString());
        taskEndDay.setSelectedIndex(c.get(Calendar.DATE) - 1);



    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField actorField;
    private javax.swing.JButton approveButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton rejectButton;
    private javax.swing.JTextArea taskDescription;
    private javax.swing.JComboBox taskEndDay;
    private javax.swing.JFormattedTextField taskEndHour;
    private javax.swing.JFormattedTextField taskEndMinute;
    private javax.swing.JComboBox taskEndMonth;
    private javax.swing.JComboBox taskEndYear;
    private javax.swing.JTextField taskName;
    private javax.swing.JComboBox taskStartDay;
    private javax.swing.JFormattedTextField taskStartHour;
    private javax.swing.JFormattedTextField taskStartMinute;
    private javax.swing.JComboBox taskStartMonth;
    private javax.swing.JComboBox taskStartYear;
    // End of variables declaration//GEN-END:variables
}
